package com.wyw.hemerocallis.config;

import feign.Logger;
import feign.Request;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;
/**
 * 类/方法简述
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Configuration
public class FeignConfig {

    /**
     * 开启Open Feign 日志
     */
    @Bean
    public Logger.Level feignLogger() {
        return Logger.Level.FULL;//日志级别需要是debug
    }

    /**
     * OpenFeign开启重试策略
     * period=100   发起当前请求的时间间隔，单位是ms
     * maxPeriod = 1000 发起当前请求的最大时间间隔，单位是ms
     * maxAttempts = 5  最大的请求次数
     */
    public Retryer feignRetryer() {
        return new Retryer.Default(100,
                SECONDS.toMillis(1),
                5);
    }

    public static final int CONNECT_TIMEOUT_MILLS = 5000;
    public static final int READ_TIMEOUT_MILLS = 5000;

    /**
     * 对请求的连接和响应时间进行限制
     *  followRedirects  请求中若是有转发在内，是否也包括在这个时间内
     */
    public Request.Options options() {
        return new Request.Options(
                CONNECT_TIMEOUT_MILLS, TimeUnit.MICROSECONDS,
                READ_TIMEOUT_MILLS, TimeUnit.MILLISECONDS,
                true
        );
    }

}
